package client.logic.connect;

/*Copyright 2010 Dietmar Wieser, Margit Mutschlechner and Florian Häser. All rights reserved.

 Redistribution and use in source and binary forms, with or without modification, are
 permitted provided that the following conditions are met:

 1. Redistributions of source code must retain the above copyright notice, this list of
 conditions and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright notice, this list
 of conditions and the following disclaimer in the documentation and/or other materials
 provided with the distribution.

 THIS SOFTWARE IS PROVIDED BY Dietmar Wieser, Margit Mutschlechner and Florian Häser ``AS IS''
 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 Dietmar Wieser, Margit Mutschlechner and Florian Häser OR
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 The views and conclusions contained in the software and documentation are those of the
 authors and should not be interpreted as representing official policies, either expressed
 or implied, of Dietmar Wieser, Margit Mutschlechner and Florian Häser.
 */
import android.util.Log;
import client.logic.clientmodel.ClientAuthorResponse;
import client.logic.clientmodel.ClientConferenceResponse;
import client.logic.clientmodel.ClientLoginResponse;
import client.logic.clientmodel.ClientProfile;
import client.logic.clientmodel.ClientPublicationsResponse;

/**
 * Class to send requests from the client to the server.
 * 
 * @author Dietmar Wieser
 * 
 */
public class ServerConnector {

	/**
	 * Sends a request for all active conferences.
	 * 
	 * @return all active conferences as string array
	 */
	public static String[] requestConferences() {
		byte[] b = MyHttpGet.requestHttpGet("?type=" + 1);
		ClientConferenceResponse ccr = new ClientConferenceResponse();
		try {
			ccr.resurrect(b);
			return ccr.getConferenceStrings();
		} catch (Exception e) {
			System.out
					.println("Expected Exception -> requestHttpGet returned null");
		}
		return null;
	}

	/**
	 * Sends a request for the suggested authors.
	 * 
	 * @param profile
	 *            the profile of the client
	 * @return all suggested authors
	 */
	public static ClientAuthorResponse requestSuggestedAuthors(
			ClientProfile profile) {
		byte[] b = MyHttpGet.requestHttpGet("?type=" + 3 + "&authorId="
				+ profile.getAuthorId() + "&mac=" + profile.getMacAddress()
				+ "&conference=" + profile.getConference());
		ClientAuthorResponse car = new ClientAuthorResponse();
		try {

			car.resurrect(b);
			return car;
		} catch (Exception e) {
			e.printStackTrace();
			System.out
					.println("Expected Exception -> requestHttpGet returned null");
		}
		return null;
	}

	/**
	 * Sends a login request to the server.
	 * 
	 * @param firstName
	 *            the first name of the client
	 * @param lastName
	 *            the last name of the client
	 * @return the response of the server with all possible names that are like
	 *         the given first and last name
	 */
	public static ClientLoginResponse requestLogin(String firstName,
			String lastName) {
		byte[] b = MyHttpGet.requestHttpGet("?type=" + 2 + "&firstName="
				+ firstName + "&lastName=" + lastName);
		ClientLoginResponse clr = new ClientLoginResponse();
		try {
			clr.resurrect(b);
			return clr;
		} catch (Exception e) {
			System.out
					.println("Expected Exception -> requestHttpGet returned null");
		}
		return null;
	}

	/**
	 * Sends a request for all publications of an author.
	 * 
	 * @param author
	 *            the name of the author
	 * @return all publications of the author in form of a string array
	 */
	public static String[] requestPublications(String author) {
		byte[] b = MyHttpGet.requestHttpGet("?type=" + 4 + "&author=" + author);
		ClientPublicationsResponse cpr = new ClientPublicationsResponse();
		try {
			cpr.resurrect(b);
			return cpr.getPublicationStrings();
		} catch (Exception e) {
			System.out
					.println("Expected Exception -> requestHttpGet returned null");
		}
		return null;
	}

}
